﻿using System.Data;
using DataScripter.Scripting;
using DataScripter.Scripting.Output;
using DataScripter.Scripting.Output.Sql.SqlServer;
using DataScripter.Scripting.OutputDestinations;
using DataScripter.Scripting.ScriptDefinitions;
using DataScripter.UnitTests.Factories;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DataScripter.UnitTests.OutputProducerTests.Sql.SqlServer.Insert
{
    [TestClass]
    public class WhenReaderHasNoRecords
    {
        private readonly MemoryScriptResult _scriptResult;

        public WhenReaderHasNoRecords()
        {
            Engine engine = new Engine();
            IOutputDestination outputDestination = new FileOutputDestination("", engine);
            engine.RegisterType(outputDestination);

            InsertOutputProducer insertOutputProducer = new InsertOutputProducer(outputDestination);
            IScriptDefinition scriptDefinition = new ScriptDefinition("", engine);
            engine.RegisterType(scriptDefinition);

            _scriptResult = new MemoryScriptResult();
            insertOutputProducer.InitializeScript(engine, _scriptResult);
            
            Mock<IDataReader> readerMock = new Mock<IDataReader>();
            ITableScriptRequest tableScriptRequest = scriptDefinition.InstallScriptRequest("abc");
            tableScriptRequest.UsedQuery = "myQuery";
            insertOutputProducer.ProduceOutput(readerMock.Object, tableScriptRequest);
            insertOutputProducer.FinalizeScript();
            _scriptResult.Close(); // performed by engine itself - don't use in engine client code
        }

        [TestMethod]
        public void ExpectedOutputShouldNotContainInsertStatements()
        {
            const string expected = @"SET XACT_ABORT ON
SET NOCOUNT ON
BEGIN TRANSACTION
/*
TABLE: abc
QUERY: myQuery
*/
--No records for abc

COMMIT TRANSACTION
";
            Assert.AreEqual(expected, _scriptResult.Output);
        }
    }
}
